package com.ics.cmsadmin.modules.api.controller;

import com.ics.cmsadmin.frame.core.bean.ApiResponse;
import com.ics.cmsadmin.frame.core.bean.PageResult;
import com.ics.cmsadmin.modules.basic.bean.StudentBean;
import com.ics.cmsadmin.modules.basic.service.StudentService;
import com.ics.cmsadmin.modules.sso.utils.SsoUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/student")
public class ApiStudentController {

    private final static String STUDENT_EXIST = "exist";

    @Autowired
    private StudentService studentService;

    @PostMapping("/check")
    public ApiResponse check(@RequestBody StudentBean studentBean){
        PageResult pageResult = studentService.list(studentBean, null);
        boolean b = !CollectionUtils.isEmpty((Collection) pageResult.getDataList());
//        Map<String, Boolean> map = new HashMap<>();
//        map.put(STUDENT_EXIST, b);
        return ApiResponse.getDefaultResponse(b);
    }

    @GetMapping("/{id}")
    public ApiResponse queryById(@PathVariable String id) {
        StudentBean studentBean = studentService.queryById(id);
        return ApiResponse.getDefaultResponse(studentBean);
    }

    @GetMapping("/queryStudentInfo")
    public ApiResponse queryStudentInfo(HttpServletRequest request){
        String userId = SsoUtils.getLoginUserId(request);
        return ApiResponse.getDefaultResponse(studentService.queryById(userId));
    }

    @PutMapping("/updateStudentInfo")
    public ApiResponse updateStudentInfo(@RequestBody StudentBean student){
        studentService.update(student.getId(), student);
        return ApiResponse.getDefaultResponse();
    }
}
